var now = getNowFormatDateTime(); 
var username = sessionStorage.getItem("name");
var userid = sessionStorage.getItem("userId");
var repairid=GetQueryString("repairid");
var faultphoto = [];//拍照列表
var shopphoto = [];//拍照列表
var _config ;
var opt;
var url ="../../repair_info/weixiu_add";
var insertJson ={};
$(document).ready(function(){
	$("#username").prepend(username);
	$("#repairid").append(repairid);
	$("#repairtype").change(function(){
	  opt=$("#repairtype").val();
	  show(opt);
	});
	$.ajax({
			url: '../../repair_info/weixiu_fill'
			,type:'post'
			,dataType:'json'
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({"repairid":repairid})  
			,success: function(res){
				console.log(res);
				var resdata = res.data[0];
				$('#repairdate').val(resdata.repairdate);
				$('#acceptdate').val(resdata.acceptdate);
				$('#paidansj').val(resdata.paidansj);
				if(resdata.repairid != undefined){
					url = "../../repair_info/weixiu_edit";
					$("#repairtype").val(resdata.repairtype);
					show(resdata.repairtype);
					$("#analysis").val(resdata.analysis);
					$("#plan").val(resdata.plan);
					var fphoto = resdata.faultpic.split(",");
					if(fphoto.length>0 && resdata.faultpic != ""){
						$('#faultpicUrl .clearfix').remove()
						for(var i=0;i<fphoto.length;i++){
							faultphoto.push(fphoto[i]);
							$('#faultpicUrl').append('<div class="wig_img">'
								+'<div class="img_box">'
								+'<img alt="100x100" src="'+fphoto[i]+'" onclick="showAllPic(\''+fphoto[i]+'\')"/>'
								+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
								+'</div>'
								+'</div>');
						}	
						$('#faultpicUrl').append('<div class="clearfix"></div>');
					}
					$("#returned")[0].checked=resdata.returned=="0"?"checked":"";
					$("#partsflag")[0].checked=resdata.partsflag=="0"?"checked":"";
					$("#parts").val(resdata.parts);
					$("#cost_parts").val(resdata.cost_parts);
					$("#cost_worker").val(resdata.cost_worker);
					$("#qita").val(resdata.qita);
					var sphoto = resdata.shoppic.split(",");
					if(sphoto.length>0 && resdata.shoppic != "" ){
						$('#shoppicUrl .clearfix').remove()
						for(var i=0;i<sphoto.length;i++){
							shopphoto.push(sphoto[i]);
							$('#shoppicUrl').append('<div class="wig_img">'
								+'<div class="img_box">'
								+'<img alt="100x100" src="'+sphoto[i]+'" onclick="showAllPic(\''+sphoto[i]+'\')"/>'
								+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto1(this)">'
								+'</div>'
								+'</div>');
						}	
						$('#shoppicUrl').append('<div class="clearfix"></div>');
					}
					
				}
			}
	});
});
$('#faultpic').on('click',function(){
	openphoto();
});
$('#shoppic').on('click',function(){
	openphoto1();
});
//打开钉钉拍照
function openphoto(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			faultphoto.push(result[0]);
			$('#faultpicUrl .clearfix').remove()
			$('#faultpicUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
					+'</div>'
					+'</div>');
			$('#faultpicUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}	
//打开钉钉拍照
function openphoto1(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			shopphoto.push(result[0]);
			$('#shoppicUrl .clearfix').remove()
			$('#shoppicUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic1(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto1(this)">'
					+'</div>'
					+'</div>');
			$('#shoppicUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}
//删除图片
function deletePhoto(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		faultphoto.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//删除图片
function deletePhoto1(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		shopphoto.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//预览图片
function showAllPic(imgOne){
	dd.biz.util.previewImage({
		urls: faultphoto,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}
//预览图片
function showAllPic1(imgOne){
	dd.biz.util.previewImage({
		urls: shopphoto,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}
function goback(){
	history.go(-1);
}
function finish(flag){
		insertJson.repairid=repairid;
		insertJson.repairtype=$('#repairtype').val();
		insertJson.analysis=$('#analysis').val();
		insertJson.plan=$('#plan').val();
		insertJson.lrr=userid;
	 if($('#repairtype').val()=='2'){
		insertJson.reply=$('#reply').val();
	 }else if($('#repairtype').val()=='3'){
		insertJson.faultpic = faultphoto.join(',');
		insertJson.shoppic = shopphoto.join(',');
		insertJson.returned = getflag($("#returned")[0].checked);
		insertJson.partsflag = getflag($("#partsflag")[0].checked);
		insertJson.parts = $('#parts').val();
		insertJson.cost_worker = $('#cost_worker').val();
		insertJson.cost_parts = $('#cost_parts').val();
		insertJson.qita = $('#qita').val();
	 }
	console.log(insertJson);
	if($('#repairtype').val()==3 && flag ==3){
			if($("#agree")[0].checked){
				$.ajax({
					url: url
					,type:'post'
					,dataType:'json'
					,async: false
					,contentType: "application/json; charset=utf-8"
					,data:JSON.stringify(insertJson) 
					,success: function(res){
						finish2(flag);
					}					
				});
			}else{
				layer.msg("请确认客户清楚并认同维修过程和结果及费用");
			}
	}else{
		    $.ajax({
				url: url
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(insertJson) 
				,success: function(res){
					finish2(flag);
				}					
			});
		}	

}
function finish2(flag){
		$.ajax({
		url: '../../repair_info/jiedan'
		,type:'post'
		,dataType:'json'
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({'repairid':repairid,'flag':flag})  
		,success: function(res){
			if(res.status==1002){
						layer.msg("操作成功",{time:1000},function(){
							var url= "../../mobile_page/weixiu/weixiu_chuli";
							window.location.href = url;
						});
					}
		}
	});
}
function getflag(flag){
	if(flag){return 0;}else{return 1;}
}
function show(opt){
	  if(opt=='1'){
		$("#replydiv")[0].style.display="none";
		$("#weixiudiv")[0].style.display="none";
		$("#button1")[0].style.display="";
		$("#button2")[0].style.display="none";
	  }else if(opt=='2'){
		$("#replydiv")[0].style.display="";
		$("#weixiudiv")[0].style.display="none";
		$("#button1")[0].style.display="";
		$("#button2")[0].style.display="none";
	  }else if(opt=='3'){
		$("#replydiv")[0].style.display="none";
		$("#weixiudiv")[0].style.display="";
		$("#button1")[0].style.display="none";
		$("#button2")[0].style.display="";
	  }
}